package mvc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.tomcat.dbcp.dbcp.DataSourceConnectionFactory;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class MemberDao {
	 private Data data;
	 private DriverManagerDataSource dataSource;
	
	 public boolean login(Data d){
		 boolean flag = false;
		 try{
			 Connection conn = dataSource.getConnection();
			 String sql = "select * from student where mid = ? and pwd =?";
			 PreparedStatement pstmt = conn.prepareStatement(sql);
			 pstmt.setString(1, d.getMid());
			 pstmt.setString(2, d.getPwd());
			 
			 ResultSet rs = pstmt.executeQuery();
			 if(rs.next()) flag = true;
		 }catch(Exception ex){
			 ex.printStackTrace();
		 }finally{
			 return flag;
		 }
	 }
	 
	 public List<Data> select(){
		List<Data> list = new ArrayList<Data>();
		try{
			Connection conn = dataSource.getConnection();
			String sql = "select * from student";
			PreparedStatement pstmt = conn.prepareStatement(sql);
			
			ResultSet rs = pstmt.executeQuery();
			while(rs.next()){
				Data d = new Data();
				d.setMid(rs.getString("mid"));
				d.setPwd(rs.getString("pwd"));
				list.add(d);
			}
			/*System.out.println(list.size()); list의 에대한 정보를 확인됨..*/
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			return list;
		}
	} 
	 
	public Data getData() {
		return data;
	}
	public void setData(Data data) {
		this.data = data;
	}
	public DriverManagerDataSource getDataSource() {
		return dataSource;
	}
	public void setDataSource(DriverManagerDataSource dataSource) {
		this.dataSource = dataSource;
	}
	
}
/*Data data
DriverManagerDataSource dataSource;
Setter/Getter
public boolean login(Data d){...}*/